package com.app.micaihu.view.home.original.adapter.provider

import android.text.TextUtils
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.app.micaihu.R
import com.app.micaihu.bean.news.NewsEntity
import com.app.micaihu.configure.UmEventId
import com.app.micaihu.utils.InteractiveUtils
import com.app.micaihu.utils.MobclickAgentUtils
import com.app.micaihu.videoplayer.JCUserAction
import com.app.micaihu.videoplayer.JCUserActionStandard
import com.app.micaihu.videoplayer.JCVideoPlayer
import com.app.micaihu.videoplayer.JCVideoPlayerStandard
import com.app.micaihu.view.home.original.adapter.OriginalListAdapter
import com.app.micaihu.view.utils.loadCircleImage
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 *
 */
class OriginalVideoProvider : BaseItemProvider<NewsEntity>(), JCUserActionStandard {

    override val itemViewType: Int
        get() = OriginalListAdapter.itemVideo
    private var tvPlayNum: TextView? = null
    override val layoutId: Int
        get() = R.layout.original_list_item_video

    override fun convert(holder: BaseViewHolder, item: NewsEntity) {
        MobclickAgentUtils.event(UmEventId.videolist_exposed, UmEventId.video)
        holder.getView<ImageView>(R.id.ivAuthorImg).loadCircleImage(item.authorHeadPic, R.drawable.user_head_def)
        tvPlayNum = holder.getView<TextView>(R.id.tvPlayNum)
        val jcVideoPlayer = holder.getView<JCVideoPlayerStandard>(R.id.item_videoplayer)
        jcVideoPlayer.setWidthRatio(2)
        jcVideoPlayer.setHeightRatio(1)
        jcVideoPlayer.setJcUserAction(this)
        holder.setText(R.id.tvCommentNum, item.commentNum)

        holder.setText(R.id.tvAuthor, item.authorName)
        //加载视频
        jcVideoPlayer.setShowTitle(true)
        if (item.articleThumb != null && item.articleThumb.isNotEmpty()) {
            jcVideoPlayer.setUp(item.videoCurl, JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    item.articleTitle, item.articleThumb[0])
        } else {
            jcVideoPlayer.setUp(item.videoCurl, JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    item.articleTitle, "")
        }
        jcVideoPlayer.setShareBean(item, item.urlStatus)
        if (!jcVideoPlayer.isPlaying && !TextUtils.isEmpty(item.clickNum)) {
            tvPlayNum?.text = item.clickNum
            tvPlayNum?.visibility = View.VISIBLE
        } else {
            tvPlayNum?.visibility = View.GONE
        }

        val tvZan = holder.getView<TextView>(R.id.tvZan)
        tvZan.text = item.praiseNum
        val flLaudContainer = holder.getView<FrameLayout>(R.id.flLaudContainer)
        if (InteractiveUtils.getInstance().theLaudIsContains(item.articleId, true)) {
            tvZan.isEnabled = false
            flLaudContainer.tag = true
        } else {
            tvZan.isEnabled = true
            flLaudContainer.tag = item
        }
        flLaudContainer.setOnClickListener {
            if (flLaudContainer.tag == null) {
                return@setOnClickListener
            }
            InteractiveUtils.getInstance().laud((flLaudContainer), null)
        }
//        val view = ViewVideo(holder.itemView)
//        view.loadData(item)
    }

    override fun onEvent(type: Int, url: String?, screen: Int, vararg objects: Any?) {
        if (type == JCUserAction.ON_CLICK_START_ICON || type == JCUserActionStandard.ON_CLICK_START_THUMB) {
            if (tvPlayNum != null && tvPlayNum?.visibility == View.VISIBLE) {
                this.tvPlayNum?.visibility = View.GONE
            }
        }
    }

}